﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.IO;

namespace HGLLCP
{
    public partial class DetailList : PhoneApplicationPage
    {
        public DetailList()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

            IDictionary<string, string> parameters = this.NavigationContext.QueryString;

            if (parameters.ContainsKey("c"))
            {
                string strC = parameters["c"];
                if (!String.IsNullOrEmpty(strC))
                {
                    switch (strC)
                    {
                        case "152":
                            pivot1.SelectedIndex = 0;
                            break;
                        case "153":
                            pivot1.SelectedIndex = 1;
                            break;
                        case "154":
                            pivot1.SelectedIndex = 2;
                            break;
                        case "155":
                            pivot1.SelectedIndex = 3;
                            break;
                        case "156":
                            pivot1.SelectedIndex = 4;
                            break;
                    }
                }
            }
            string jsonDetailList = "";
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                //152
                if (myIsolatedStorage.FileExists("assets/newslist_152.txt"))
                {
                    using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("assets/newslist_152.txt", FileMode.Open, FileAccess.Read))
                    {
                        if (stream.Length > 0)
                        {
                            using (StreamReader reader = new StreamReader(stream))
                            {
                                jsonDetailList = reader.ReadLine();
                            }
                        }
                    }
                }
                if (!String.IsNullOrEmpty(jsonDetailList))
                {
                    List<NewsList> listDetail1 = JsonHelper.Deserialize<List<NewsList>>(jsonDetailList);
                    list1.ItemsSource = listDetail1;
                }
                //153
                if (myIsolatedStorage.FileExists("assets/newslist_153.txt"))
                {
                    using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("assets/newslist_153.txt", FileMode.Open, FileAccess.Read))
                    {
                        if (stream.Length > 0)
                        {
                            using (StreamReader reader = new StreamReader(stream))
                            {
                                jsonDetailList = reader.ReadLine();
                            }
                        }
                    }
                }
                if (!String.IsNullOrEmpty(jsonDetailList))
                {
                    List<NewsList> listDetail2 = JsonHelper.Deserialize<List<NewsList>>(jsonDetailList);
                    list2.ItemsSource = listDetail2;
                }
                //154
                if (myIsolatedStorage.FileExists("assets/newslist_154.txt"))
                {
                    using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("assets/newslist_154.txt", FileMode.Open, FileAccess.Read))
                    {
                        if (stream.Length > 0)
                        {
                            using (StreamReader reader = new StreamReader(stream))
                            {
                                jsonDetailList = reader.ReadLine();
                            }
                        }
                    }
                }
                if (!String.IsNullOrEmpty(jsonDetailList))
                {
                    List<NewsList> listDetail3 = JsonHelper.Deserialize<List<NewsList>>(jsonDetailList);
                    list3.ItemsSource = listDetail3;
                }
                //155
                if (myIsolatedStorage.FileExists("assets/newslist_155.txt"))
                {
                    using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("assets/newslist_155.txt", FileMode.Open, FileAccess.Read))
                    {
                        if (stream.Length > 0)
                        {
                            using (StreamReader reader = new StreamReader(stream))
                            {
                                jsonDetailList = reader.ReadLine();
                            }
                        }
                    }
                }
                if (!String.IsNullOrEmpty(jsonDetailList))
                {
                    List<NewsList> listDetail4 = JsonHelper.Deserialize<List<NewsList>>(jsonDetailList);
                    list4.ItemsSource = listDetail4;
                }
                //156
                if (myIsolatedStorage.FileExists("assets/newslist_156.txt"))
                {
                    using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("assets/newslist_156.txt", FileMode.Open, FileAccess.Read))
                    {
                        if (stream.Length > 0)
                        {
                            using (StreamReader reader = new StreamReader(stream))
                            {
                                jsonDetailList = reader.ReadLine();
                            }
                        }
                    }
                }
                if (!String.IsNullOrEmpty(jsonDetailList))
                {
                    List<NewsList> listDetail5 = JsonHelper.Deserialize<List<NewsList>>(jsonDetailList);
                    list5.ItemsSource = listDetail5;
                }
            }
        }

        private void list1_Tap(object sender, GestureEventArgs e)
        {
            if (((ListBox)sender).SelectedIndex > -1)
                NavigationService.Navigate(new Uri(String.Format("/Detail.xaml?d={0}", (((ListBox)sender).SelectedValue as NewsList).id), UriKind.Relative));
        }
    }
}